const json5 = require('json5');
const fs = require('fs');
const path = require('path');

const readJson = {
  /**
   * 读取带注释的Json文件
   * @param {*} jsonFilePath
   * @returns
   */
  readJsonFile(jsonFilePath) {
    try {
      // 读取带注释的 JSON 文件
      const data = fs.readFileSync(jsonFilePath, 'utf8');
      // 使用 json5 库解析带注释的 JSON 数据
      const parsedData = json5.parse(data);
      return parsedData;
    } catch (error) {
      console.error('Error reading or parsing JSON file:', error);
      throw error;
    }
  },
  getPagesJSonFiles(jsonPath = 'src/pages.json') {
    const filePaths = [];
    const pagesJsonFile = readJson.readJsonFile(path.resolve(process.cwd(), jsonPath)); // 替换成你的 pages.json 路径
    for (const v of pagesJsonFile.pages) {
      const pageFilePath = path.resolve(process.cwd(), `src/${v.path}.vue`);
      filePaths.push(pageFilePath);
    }
    for (const p of pagesJsonFile.subPackages) {
      for (const v of p.pages) {
        const pageFilePath = path.resolve(process.cwd(), `src/${p.root}/${v.path}.vue`);
        filePaths.push(pageFilePath);
      }
    }
    console.log(" >> getPagesJSonFiles > 读取到pages.json 有", filePaths.length, "个路由页面文件");
    return filePaths;
  }
};

module.exports = readJson;
